﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MyConnection;
using Usefulls;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
namespace Hotel_Price
{
    public partial class frmHotelLinkList : DevExpress.XtraEditors.XtraForm
    {
        RepositoryItemComboBox lueAcenta = new RepositoryItemComboBox();
        IRDataSource data_Link, data_LinkControl;
        public frmHotelLinkList()
        {
            InitializeComponent();
            this.Load += frmHotelLinkList_Load;
            this.gridView1.Click += gridView1_Click;
            this.btnSave.ItemClick += btnSave_ItemClick;
            this.btnClose.ItemClick += btnClose_ItemClick;
            this.btnGrid1Sil.Click += btnGrid1Sil_Click;
            this.gridView2.InitNewRow += gridView2_InitNewRow;

            lueAcenta.Items.Add("JOL");
            lueAcenta.Items.Add("ANI");
            lueAcenta.Items.Add("TTB");
            lueAcenta.Items.Add("TTC");
            lueAcenta.Items.Add("SET");
            lueAcenta.Items.Add("TEZ");
            lueAcenta.Items.Add("VIP");
            lueAcenta.Items.Add("HMS");

            gridControl1.ContextMenuStrip = contextMenuStrip1;
        }

        void gridView2_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                gridView2.SetRowCellValue(gridView2.FocusedRowHandle, "inMasterID", gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "inID").ToString());
            }
            
        }

        void btnGrid1Sil_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle>-1)
            {
                MyDBTools.RunSQL(string.Format("Delete From THotel_Link Where inID={0}", gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "inID").ToString()));
                MyTools.IRShowMessage("Kayıt Silindi");
                LoadLink();
            }
        }

        void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool kontrol = false;
            try
            {
                data_Link.Save();
                data_LinkControl.Save();
                kontrol = true;
            }
            catch
            {
                MyTools.IRShowMessage(MyTools.ErrMsg);
            }
            if (kontrol)
            {
                MyTools.IRShowMessage(MyTools.SaveMsg);
            }
            
        }

        void gridView1_Click(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                string value = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "inID").ToString();
                int outValue = -1;
                if (int.TryParse(value, out outValue))
                {
                    LoadLinkControl(outValue);
                }
            }
           
        }

        void frmHotelLinkList_Load(object sender, EventArgs e)
        {
            LoadLink();
        }

        #region Procedureler

        void LoadLink()
        {
            data_Link = new IRDataSource("Select * From THotel_Link");
                gridView1.Columns.Clear();
                gridControl1.DataBindings.Clear();
                gridControl1.DataSource = data_Link.bs;
                MyTools.GridVisibleCols(gridView1);
                MyTools.IRSetGridColumn(gridView1, 1, "stName", "Otel Adı", true, 100);
                MyTools.IRSetGridColumn(gridView1, 2, "stLink", "MNG Linki", true, 100);
                
                for (int i = 0; i < gridView1.Columns.Count; i++)
                {
                    if (gridView1.Columns[i].FieldName != "stLink")
                    {
                        gridView1.Columns[i].OptionsColumn.AllowEdit = false;
                        gridView1.Columns[i].OptionsColumn.ReadOnly = true;
                    }
                }

                gridView1.BestFitColumns();
        }

        void LoadLinkControl(int inID)
        {
            data_LinkControl = new IRDataSource(string.Format("Select * From THotel_Link_Control Where inMasterID={0}", inID));
            gridView2.Columns.Clear();
            gridControl2.DataBindings.Clear();
            gridControl2.DataSource = data_LinkControl.bs;
            MyTools.GridVisibleCols(gridView2);
            MyTools.IRSetGridColumn(gridView2, 1, "stHotelCode", "Acenta Kodu", true, 50);
            MyTools.IRSetGridColumn(gridView2, 2, "stLink", "Otel Linki", true, 100);
            gridView2.Columns["stHotelCode"].ColumnEdit = lueAcenta;
        }

        #endregion
        
     

    }
}